<?php

namespace app\xwam\model;

/**
 * Description of SessionFilterModel
 *
 * @author SoChishun
 * @version 2017-3-13
 */
class LoginFilterModel {

    /**
     * 登录错误次数
     * @var string
     */
    protected static $name = 'login_error_count';

    /**
     * 设置登录错误次数
     * @version 2017-3-13
     */
    public function setLoginErrorInfo() {
        $data = session(self::$name);
        if (is_array($data)) {
            $n = $data['count'];
        } else {
            $n = 0;
        }
        $n++;
        session(self::$name, array('count' => $n, 'time' => strtotime('5 min')));
    }

    /*
     * 获取登陆错误次数
     * @version 2018-1-29
     */

    public function getLoginErrorCount() {
        $data = session(self::$name);
        if ($data && $data['time'] < time()) {
            return intval($data['count']);
        }
        return 0;
    }

    /**
     * 获取登录操作状态
     * @return boolean
     * @version 2017-3-13
     */
    public function checkLoginable() {
        $data = session(self::$name);
        if ($data && $data['count'] > 5) {
            if ($data['time'] > time()) {
                return false;
            }
            session(self::$name, null); // 清空
        }
        return true;
    }

    /**
     * 重置登录错误信息
     * @version 2018-1-29
     */
    public function resetLoginErrorInfo() {
        session(self::$name, null);
    }

}
